package net.cyl.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.cyl.controller.request.LinkGroupAddRequest;
import net.cyl.controller.request.LinkGroupUpdateRequest;
import net.cyl.interceptor.LoginInterceptor;
import net.cyl.manager.LinkGroupManager;
import net.cyl.model.LinkGroupDO;
import net.cyl.mapper.LinkGroupMapper;
import net.cyl.service.LinkGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.cyl.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 曹玉林
 * @since 2025-05-14
 */
@Service
@Slf4j
public class LinkGroupServiceImpl extends ServiceImpl<LinkGroupMapper, LinkGroupDO> implements LinkGroupService {


    @Autowired
    private LinkGroupManager linkGroupManager;
    @Override
    public int add(LinkGroupAddRequest addRequest) {
        Long accountNo= LoginInterceptor.loginUserThreadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setTitle(addRequest.getGroupName());
        return linkGroupManager.add(linkGroupDO);
    }

    @Override
    public int  delete(Long id) {
        Long accountNo= LoginInterceptor.loginUserThreadLocal.get().getAccountNo();
        return linkGroupManager.delete(id,accountNo);
    }

    @Override
    public LinkGroupVO detail(Long groupId) {
        Long accountNo= LoginInterceptor.loginUserThreadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId,accountNo);
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> findUserAllLinkGroup() {
        Long accountNo= LoginInterceptor.loginUserThreadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList = linkGroupManager.findUserAllLinkGroup(accountNo);

        return linkGroupDOList.stream().map(linkGroupDO -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());
    }

    @Override
    public int updateByIdf(LinkGroupUpdateRequest request) {
        Long accountNo= LoginInterceptor.loginUserThreadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setId(request.getId());
        linkGroupDO.setTitle(request.getGroupName());
        linkGroupDO.setAccountNo(accountNo);
        return linkGroupManager.updateByIdf(linkGroupDO);
    }
}
